<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>keepalived 的启动脚本和安装脚本 的结合配置文件详解 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>keepalived 的启动脚本和安装脚本 的结合配置文件详解</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/LVS/">LVS</a> <a class="article__tag-link" href="/tags/keepalived/">keepalived</a>
			</span>
		
	</div>

	

	
		<h2 id="keepalived-的启动脚本"><a href="#keepalived-的启动脚本" class="headerlink" title="keepalived 的启动脚本"></a>keepalived 的启动脚本</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># Startup script for the Keepalived daemon</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># processname: keepalived</span></div><div class="line"><span class="comment"># pidfile: /var/run/keepalived.pid</span></div><div class="line"><span class="comment"># config: /etc/keepalived/keepalived.conf</span></div><div class="line"><span class="comment"># chkconfig: - 21 79</span></div><div class="line"><span class="comment"># description: Start and stop Keepalived</span></div><div class="line"></div><div class="line">. /etc/init.d/<span class="built_in">functions</span></div><div class="line"></div><div class="line"><span class="comment"># Source configuration file (we set KEEPALIVED_OPTIONS there)</span></div><div class="line"><span class="comment">#. /etc/sysconfig/keepalived</span></div><div class="line">KEEP_CONF=<span class="string">"/export/servers/keepalived-1.2.13/conf/keepalived.conf"</span></div><div class="line">KEEP_SBIN=<span class="string">"/export/servers/keepalived-1.2.13/sbin/keepalived"</span></div><div class="line">KEEP_PID=<span class="string">"/var/run/keepalived.pid "</span></div><div class="line"></div><div class="line">RETVAL=0</div><div class="line"></div><div class="line">prog=<span class="string">"keepalived"</span></div><div class="line"></div><div class="line"><span class="function"><span class="title">start</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Starting <span class="variable">$prog</span>: "</span></div><div class="line">    daemon <span class="variable">$KEEP_SBIN</span> --use-file=<span class="variable">$KEEP_CONF</span></div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">stop</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Stopping <span class="variable">$prog</span>: "</span></div><div class="line">    killproc -p <span class="variable">$KEEP_PID</span> <span class="variable">$KEEP_SBIN</span> -TERM</div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="comment">#[ $RETVAL -eq 0 ] &amp;&amp; rm -f /var/lock/subsys/$prog</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">reload</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Reloading <span class="variable">$prog</span>: "</span></div><div class="line">    <span class="comment">#killproc keepalived -1</span></div><div class="line">    killproc -p <span class="variable">$KEEP_PID</span> <span class="variable">$KEEP_SBIN</span> -HUP</div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment"># See how we were called.</span></div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line">    start)</div><div class="line">        start</div><div class="line">        ;;</div><div class="line">    stop)</div><div class="line">        stop</div><div class="line">        ;;</div><div class="line">    reload)</div><div class="line">        reload</div><div class="line">        ;;</div><div class="line">    restart)</div><div class="line">        stop</div><div class="line">        start</div><div class="line">        ;;</div><div class="line">    condrestart)</div><div class="line">        <span class="keyword">if</span> [ -f /var/lock/subsys/<span class="variable">$prog</span> ]; <span class="keyword">then</span></div><div class="line">            stop</div><div class="line">            start</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        ;;</div><div class="line">    status)</div><div class="line">        status keepalived</div><div class="line">        RETVAL=$?</div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;start|stop|reload|restart|condrestart|status&#125;"</span></div><div class="line">        RETVAL=1</div><div class="line"><span class="keyword">esac</span></div><div class="line"></div><div class="line"><span class="built_in">exit</span> <span class="variable">$RETVAL</span></div><div class="line">=======</div><div class="line"><span class="meta">#!/bin/sh</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># Startup script for the Keepalived daemon</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment"># processname: keepalived</span></div><div class="line"><span class="comment"># pidfile: /var/run/keepalived.pid</span></div><div class="line"><span class="comment"># config: /etc/keepalived/keepalived.conf</span></div><div class="line"><span class="comment"># chkconfig: - 21 79</span></div><div class="line"><span class="comment"># description: Start and stop Keepalived</span></div><div class="line"></div><div class="line"><span class="comment"># Source function library</span></div><div class="line">. /etc/init.d/<span class="built_in">functions</span></div><div class="line"></div><div class="line"><span class="comment"># Source configuration file (we set KEEPALIVED_OPTIONS there)</span></div><div class="line"><span class="comment">#. /etc/sysconfig/keepalived</span></div><div class="line">KEEP_CONF=<span class="string">"/export/servers/keepalived-1.2.13/conf/keepalived.conf"</span></div><div class="line">KEEP_SBIN=<span class="string">"/export/servers/keepalived-1.2.13/sbin/keepalived"</span></div><div class="line">KEEP_PID=<span class="string">"/var/run/keepalived.pid "</span></div><div class="line"></div><div class="line">RETVAL=0</div><div class="line"></div><div class="line">prog=<span class="string">"keepalived"</span></div><div class="line"></div><div class="line"><span class="function"><span class="title">start</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Starting <span class="variable">$prog</span>: "</span></div><div class="line">    daemon <span class="variable">$KEEP_SBIN</span> --use-file=<span class="variable">$KEEP_CONF</span></div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">stop</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Stopping <span class="variable">$prog</span>: "</span></div><div class="line">    killproc -p <span class="variable">$KEEP_PID</span> <span class="variable">$KEEP_SBIN</span> -TERM</div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="comment">#[ $RETVAL -eq 0 ] &amp;&amp; rm -f /var/lock/subsys/$prog</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">reload</span></span>() &#123;</div><div class="line">    <span class="built_in">echo</span> -n $<span class="string">"Reloading <span class="variable">$prog</span>: "</span></div><div class="line">    <span class="comment">#killproc keepalived -1</span></div><div class="line">    killproc -p <span class="variable">$KEEP_PID</span> <span class="variable">$KEEP_SBIN</span> -HUP</div><div class="line">    RETVAL=$?</div><div class="line">    <span class="built_in">echo</span></div><div class="line">    <span class="built_in">return</span> <span class="variable">$RETVAL</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment"># See how we were called.</span></div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line">    start)</div><div class="line">        start</div><div class="line">        ;;</div><div class="line">    stop)</div><div class="line">        stop</div><div class="line">        ;;</div><div class="line">    reload)</div><div class="line">        reload</div><div class="line">        ;;</div><div class="line">    restart)</div><div class="line">        stop</div><div class="line">        start</div><div class="line">        ;;</div><div class="line">    condrestart)</div><div class="line">        <span class="keyword">if</span> [ -f /var/lock/subsys/<span class="variable">$prog</span> ]; <span class="keyword">then</span></div><div class="line">            stop</div><div class="line">            start</div><div class="line">        <span class="keyword">fi</span></div><div class="line">        ;;</div><div class="line">    status)</div><div class="line">        status keepalived</div><div class="line">        RETVAL=$?</div><div class="line">        ;;</div><div class="line">    *)</div><div class="line">        <span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;start|stop|reload|restart|condrestart|status&#125;"</span></div><div class="line">        RETVAL=1</div><div class="line"><span class="keyword">esac</span></div><div class="line"></div><div class="line"><span class="built_in">exit</span> <span class="variable">$RETVAL</span></div><div class="line">&gt;&gt;&gt;&gt;&gt;&gt;&gt; .r3360</div></pre></td></tr></table></figure>
<h2 id="keepalived-的安装脚本"><a href="#keepalived-的安装脚本" class="headerlink" title="keepalived 的安装脚本"></a>keepalived 的安装脚本</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line">role=<span class="variable">$1</span></div><div class="line">weight=<span class="variable">$2</span></div><div class="line">vip=<span class="variable">$3</span></div><div class="line"><span class="comment">#routerid=`ifconfig |grep Bcast|cut -d: -f2|awk '&#123;print $1&#125;'|awk -F. '&#123;print $4&#125;'`</span></div><div class="line">routerid=<span class="variable">$4</span></div><div class="line">interface=`ifconfig | awk -F<span class="string">"     "</span> <span class="string">'&#123;print $1&#125;'</span> | grep -v lo | grep -v ^\s*$`</div><div class="line"><span class="comment">#vrrd_id=`ifconfig |grep "inet addr"|grep -v "127.0.0.1"|awk -F: '&#123;print $2&#125;'|awk '&#123;print $1&#125;'|awk -F. '&#123;print $4&#125;'`</span></div><div class="line">keep_dir=/<span class="built_in">export</span>/servers/keepalived-1.2.13</div><div class="line">conf_dir=<span class="variable">$keep_dir</span>/conf</div><div class="line">pid_dir=<span class="variable">$keep_dir</span>/pid</div><div class="line">sh_dir=/<span class="built_in">export</span>/sh</div><div class="line">WHO=`whoami`</div><div class="line"><span class="keyword">function</span> <span class="built_in">help</span>()</div><div class="line">&#123;</div><div class="line">	<span class="built_in">echo</span> -e <span class="string">"Install Tips:#Such as master安装 sh <span class="variable">$0</span> MASTER 100 vip routerid # BACKUP安装 sh <span class="variable">$0</span> BACKUP [LESS THEN 100] vip routerid"</span></div><div class="line">&#125;</div><div class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> -eq 0 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">	<span class="built_in">help</span></div><div class="line">	<span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="comment">########检查网络，安装popt包</span></div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32mCheckNetwork\033[0m"</span></div><div class="line"><span class="keyword">if</span> ! ping -c 1 -w 1 172.22.197.62 &gt; /dev/null; <span class="keyword">then</span></div><div class="line">      <span class="built_in">echo</span> -e <span class="string">"\033[;37;31mPlease Check Network Before Setup\033[0m"</span>;</div><div class="line">      <span class="built_in">exit</span> 1;</div><div class="line"><span class="keyword">else</span> </div><div class="line">      <span class="built_in">echo</span> -e <span class="string">"\033[;37;32mOK\033[0m"</span>;</div><div class="line"><span class="keyword">fi</span></div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32mStarting Yum\033[0m"</span></div><div class="line">yum -y install popt-devel openssl openssl-devel make openssl-devel popt popt-devel libnl-devel kernel-devel</div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32mYum Install Done\033[0m"</span></div><div class="line"><span class="comment">####download and install keepalived-1.2.13.tar.gz</span></div><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/src/keepalived</div><div class="line"><span class="keyword">if</span> [ ! -e keepalived-1.2.13.tar.gz ]</div><div class="line"><span class="keyword">then</span></div><div class="line">	wget http://172.22.197.62/CentOS/app/keepalived-1.2.13.tar.gz</div><div class="line">	tar xzf keepalived-1.2.13.tar.gz</div><div class="line"><span class="keyword">else</span></div><div class="line">	tar xzf keepalived-1.2.13.tar.gz</div><div class="line"><span class="keyword">fi</span></div><div class="line">rpm -ivh popt-static-1.13-7.el6.x86_64.rpm</div><div class="line"><span class="built_in">cd</span> keepalived-1.2.13</div><div class="line">./configure --prefix=/<span class="built_in">export</span>/servers/keepalived-1.2.13 --sysconfdir=/<span class="built_in">export</span>/servers/keepalived-1.2.13/etc/ --bindir=/<span class="built_in">export</span>/servers/keepalived-1.2.13/bin/ --with-kernel-dir=/usr/src/linux/</div><div class="line">make &amp;&amp; make install</div><div class="line"><span class="keyword">if</span> [ $? -eq 0 ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">		<span class="built_in">echo</span> <span class="string">"make success"</span></div><div class="line">	<span class="keyword">else</span></div><div class="line">		<span class="built_in">echo</span> <span class="string">"make error"</span></div><div class="line">	  <span class="built_in">exit</span> 1</div><div class="line"><span class="keyword">fi</span></div><div class="line">cp /<span class="built_in">export</span>/servers/keepalived-1.2.13/sbin/keepalived /usr/sbin/</div><div class="line">cp /<span class="built_in">export</span>/servers/keepalived-1.2.13/etc/sysconfig/keepalived /etc/sysconfig/</div><div class="line">cp ../keepalived /etc/init.d/</div><div class="line">chmod +x /etc/init.d/keepalived</div><div class="line">chkconfig --add keepalived</div><div class="line">chkconfig keepalived on</div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32m创建keepalive配置文件路径\033[0m"</span></div><div class="line">mkdir -p <span class="variable">$conf_dir</span></div><div class="line"><span class="comment">#######添加keepalive配置文件</span></div><div class="line">cat &gt; <span class="variable">$conf_dir</span>/keepalived.conf &lt;&lt;EOF</div><div class="line">! Configuration File <span class="keyword">for</span> keepalived</div><div class="line"></div><div class="line">global_defs &#123;</div><div class="line">   notification_email &#123;</div><div class="line">      lihuiyw@jd.com   </div><div class="line">&#125;</div><div class="line">   notification_email_from lihuiyw@jd.com</div><div class="line">   smtp_server mail.jd.com</div><div class="line">   smtp_connect_timeout 30</div><div class="line">   router_id LVS_DEVEL</div><div class="line">&#125;</div><div class="line">vrrp_script check_alive</div><div class="line">&#123;</div><div class="line">        script <span class="string">"/export/sh/check_nginx_alive.sh"</span></div><div class="line">        <span class="comment"># check every 2 seconds</span></div><div class="line">        interval 2</div><div class="line">        <span class="comment"># if failed, decrease 10 of the priority</span></div><div class="line">        weight -10</div><div class="line">        <span class="comment"># require 2 failures for failures</span></div><div class="line">        fail 2</div><div class="line">        <span class="comment"># require 1 sucesses for ok</span></div><div class="line">        rise 1</div><div class="line">&#125;</div><div class="line">vrrp_instance VIP_<span class="variable">$&#123;routerid&#125;</span></div><div class="line">&#123;</div><div class="line">	state <span class="variable">$role</span></div><div class="line">	interface <span class="variable">$interface</span></div><div class="line">	virtual_router_id <span class="variable">$routerid</span></div><div class="line">	priority <span class="variable">$weight</span></div><div class="line">	advert_int 2 </div><div class="line">	garp_master_delay 10</div><div class="line">	smtp_alert</div><div class="line"></div><div class="line">	authentication</div><div class="line">	&#123;</div><div class="line">		auth_type PASS </div><div class="line">		auth_pass 123456</div><div class="line">	&#125;</div><div class="line">	virtual_ipaddress</div><div class="line">	&#123; </div><div class="line">		<span class="variable">$&#123;vip&#125;</span>/24</div><div class="line">	&#125;</div><div class="line">	track_interface </div><div class="line">	&#123;</div><div class="line">		<span class="variable">$interface</span></div><div class="line">	&#125; </div><div class="line">	track_script</div><div class="line">	&#123; </div><div class="line">		check_alive </div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line">EOF</div><div class="line"><span class="comment">###########监控keepalived脚本、脚本需要放到同keepalived配置文件指定路径</span></div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32m创建脚本路径\033[0m"</span></div><div class="line">mkdir -p <span class="variable">$sh_dir</span> <span class="variable">$pid_dir</span></div><div class="line">cat &gt; <span class="variable">$sh_dir</span>/check_nginx_alive.sh &lt;&lt; EOF</div><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#监控脚本功能概述：</span></div><div class="line"><span class="comment">#首先检查进程中的nginx进程数目，如果不存在（即为0）,则表示nginx未开启，然后开启nginx。</span></div><div class="line"><span class="comment">#3秒后重新检查nginx进程数，若仍为0，则表示nginx无法正常启动，此时强制停止keepalived进程，让虚拟ip切换到backup服务器上</span></div><div class="line"></div><div class="line"><span class="comment">## 如果没有nginx进程，即值为零</span></div><div class="line"><span class="comment">#echo \`pgrep -l nginx\`</span></div><div class="line"><span class="comment">#echo \`ps -ef | grep nginx\`</span></div><div class="line"></div><div class="line"><span class="keyword">if</span> [ \`pgrep <span class="string">"^nginx"</span> | wc -l\` -eq 0 ]</div><div class="line"><span class="keyword">then</span></div><div class="line">	service nginx start	</div><div class="line"></div><div class="line">	sleep 1</div><div class="line"></div><div class="line">	<span class="keyword">if</span> [ \`pgrep <span class="string">"^nginx"</span> | wc -l\` -eq 0 ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">		<span class="comment">## 则结束 keepalived 进程，使得服务器切换到BACKUP服务器上</span></div><div class="line">		service keepalived stop</div><div class="line">	<span class="keyword">fi</span></div><div class="line"><span class="keyword">fi</span></div><div class="line">EOF</div><div class="line"><span class="built_in">echo</span> -e <span class="string">"\033[;37;32m赋可执行权限\033[0m"</span></div><div class="line">chown -R admin.admin <span class="variable">$conf_dir</span> <span class="variable">$keep_dir</span> <span class="variable">$sh_dir</span></div><div class="line"><span class="comment">#chmod 777 $sh_dir/check_nginx_alive.sh</span></div><div class="line">/<span class="built_in">export</span>/servers/keepalived-1.2.13/sbin/keepalived --use-file=/<span class="built_in">export</span>/servers/keepalived-1.2.13/conf/keepalived.conf</div><div class="line">&gt;/home/install_keepalived.log</div><div class="line"><span class="keyword">if</span> [ $? -eq 0 ]</div><div class="line">	<span class="keyword">then</span></div><div class="line">		<span class="built_in">echo</span> <span class="string">"keepalived install done"</span></div><div class="line"><span class="keyword">fi</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/LVS/5. keepalived 的启动脚本和安装脚本/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
